
package com.joysuch.wwyt.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.joysuch.wwyt.core.entity.BaseOrganization;
import com.joysuch.wwyt.core.entity.BaseTenant;
import com.joysuch.wwyt.core.security.ShiroUser;
import com.joysuch.wwyt.core.support.Context;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.BeanFactory;

/**
 * SiteFilter
 * 
 * 
 */
public class TestBaseInfoFilter implements Filter {
    private BeanFactory beanFactory;

    public TestBaseInfoFilter(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public static final String SUB_SYSTEM_KEY = "_sub_system";

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        Context.setCurrentUser(createTestUser());
        chain.doFilter(request, response);
        // Context.resetCurrentSubSystem();
        Context.resetCurrentUser();
    }

    private BaseTenant createTestTenant() {
        BaseTenant t = new BaseTenant();
        t.setId(1L);
        t.setName("测试租户002");
        return t;
    }

    private ShiroUser createTestUser() {
        ShiroUser a = new ShiroUser(9753L, 9753L, "测试");
        a.setTenentId(1L);
        a.setDepartCode("001-001");
        a.setCompanyCode("001-001");
        return a;
    }

    private BaseOrganization createTestOrg() {
        BaseOrganization org = new BaseOrganization();
        org.setId(345L);
        org.setOrgCode("001-001");
        org.setCode("001-001");
        org.setName("测试结构007");
        return org;
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }

    private void addSubSystemCookie(String siteIdText, HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = new Cookie(SUB_SYSTEM_KEY, siteIdText);
        String path = request.getContextPath();
        if (StringUtils.isBlank(path)) {
            path = "/";
        }
        cookie.setPath(path);
        response.addCookie(cookie);
    }

    private void clearPermCache(HttpServletRequest request, String currSubSystemId) {
        HttpSession session = request.getSession();
        String prevSubSystemId = (String) session.getAttribute(SUB_SYSTEM_KEY);
        if (prevSubSystemId == null || !currSubSystemId.equals(prevSubSystemId)) {
            getEhCacheManager().getCacheManager().clearAll();
        }
        session.setAttribute(SUB_SYSTEM_KEY, currSubSystemId);
    }

    private EhCacheManager ehCacheManager;

    public EhCacheManager getEhCacheManager() {
        if (ehCacheManager == null) {
            ehCacheManager = beanFactory.getBean(EhCacheManager.class);
        }
        return ehCacheManager;
    }

    public void setEhCacheManager(EhCacheManager ehCacheManager) {
        this.ehCacheManager = ehCacheManager;
    }

}
